package cc.dowell.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Value("${custom-setting.file-path.root}")
    private String rootPath;

    @Value("${custom-setting.file-path.img-upload}")
    String imgUploadPath;

    @Value("${custom-setting.file-path.img-detected}")
    String imgDetectedPath;

    @Value("${custom-setting.file-path.img-carousel}")
    String imgCarouselPath;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //以下资源不验证token
        registry.addInterceptor(new TokenInterceptor())
                .excludePathPatterns("/login")
                .excludePathPatterns("/register")
                .excludePathPatterns("/changePassword")
                .excludePathPatterns("/chart")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/img2/**")
                .excludePathPatterns("/imgCarousel/**");

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String filePath = rootPath + imgUploadPath ;
        String fileDetectedPath = rootPath + imgDetectedPath ;
        String fileCarouselPath = rootPath + imgCarouselPath ;
        registry.addResourceHandler("/img/**").addResourceLocations("file:"+filePath);
        registry.addResourceHandler("/img2/**").addResourceLocations("file:"+fileDetectedPath);
        registry.addResourceHandler("/imgCarousel/**").addResourceLocations("file:"+fileCarouselPath);
    }
}